﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace Demo1
{
    class DataProvider
    {
        SqlConnection Connection;
        SqlDataAdapter DataAdapter;
        string ConnectionString = "Data Source=.;Initial Catalog={0};Integrated Security=True";
        public DataProvider(string dbName)
        {
            ConnectionString = string.Format(ConnectionString, dbName);
            Connection = new SqlConnection(ConnectionString);

        }
        public DataTable GetProducts()
        {
            DataTable da = new DataTable();
            SqlCommand cmd = new SqlCommand("select * from MATHANGG", Connection);
            DataAdapter = new SqlDataAdapter(cmd);
            DataAdapter.Fill(da);
            return da;
        }

        public int Insert(string[] abc, int check_nhap)
        {
            try
            {
                    
                Connection.Open();
                SqlCommand sql_cmd = new SqlCommand("insert into MATHANGG(MAHANG,TENHANG,MACONGTY,MALOAIHANG,SOLUONG,DONVITINH,GIABAN,GIANHAP) values ('" + abc[0] + "', N'" + abc[1] + "','" + abc[2] + "','" + abc[3] + "','" + abc[4] + "',N'" + abc[5] + "','" + abc[6] + "','" + abc[7] + "')", Connection);
                if (sql_cmd.ExecuteNonQuery() > 0)
                {
                    return check_nhap = 1;
                }
                else
                {
                    return check_nhap = 2;
                }
                Connection.Close();
                       
            }
            catch
            {
                return check_nhap = 0;
            }
        }

        public int Update(string[] abc, int check_capnhat)
        {
            try
            {

                Connection.Open();
                SqlCommand sql_cmd = new SqlCommand("update MATHANGG set TENHANG = N'" + abc[1] + "',MACONGTY = '" + abc[2] + "',MALOAIHANG = '" + abc[3] + "',SOLUONG = '" + abc[4] + "',DONVITINH = N'" + abc[5] + "',GIANHAP = '" + abc[6] + "',GIABAN = '" + abc[7] + "' where MAHANG = '" + abc[0] + "'", Connection);
                if (sql_cmd.ExecuteNonQuery() > 0)
                {
                    return check_capnhat = 1;
                }
                else
                {
                    return check_capnhat = 2;
                }
                Connection.Close();

            }
            catch
            {
                return check_capnhat = 0;
            }
        }

        public int Delected(string barcode,string tenhang, int check_xoa)
        {
            try
            {

                Connection.Open();
                SqlCommand sql_cmd = new SqlCommand("delete from MATHANGG where MAHANG = '" + barcode + "'", Connection);
                if (sql_cmd.ExecuteNonQuery() > 0)
                {
                    return check_xoa = 1;
                }
                else
                {
                    return check_xoa = 2;
                }
                Connection.Close();

            }
            catch
            {
                return check_xoa = 0;
            }
        }
        public DataTable tk_tenhang(string tenhang)
        {
            DataTable da = new DataTable();
            SqlCommand cmd = new SqlCommand("select ID,MAHANG,TENHANG from MATHANGG where TENHANG like N'" + tenhang + "%'", Connection);
            DataAdapter = new SqlDataAdapter(cmd);
            DataAdapter.Fill(da);
            return da;
        }
        public DataRow tk_barcode(string barcode)
        {
            DataTable da = new DataTable();
            SqlCommand cmd = new SqlCommand("select TENHANG,DONVITINH,GIABAN from MATHANGG where MAHANG = '" + barcode + "'", Connection);
            DataAdapter = new SqlDataAdapter(cmd);
            DataAdapter.Fill(da);
            if (da.Rows.Count > 0)
                return da.Rows[0];
            else
                return null;
        }
    }
}
